package com.jiyun.userservice.controller;

import com.jiyun.pojo.User;
import com.jiyun.userservice.serivce.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> check(@PathVariable("data")String data,@PathVariable("type") Integer type){

        Boolean bool = userService.findData(data,type);

        // true  false  null
        if(bool == null){
            // 404  没有找到
            // 400  请求内容有误
            return ResponseEntity.badRequest().build();
        }

        return ResponseEntity.ok(bool);

    }

    @PostMapping("send")
    public ResponseEntity<Void> send(String phone){

        userService.send(phone);
        return ResponseEntity.ok().build();
    }

    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, String code){

        Boolean register = userService.register(user, code);

        if(!register){
            return ResponseEntity.badRequest().build();
        }

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    @PostMapping("query")
    public ResponseEntity<User> queryUser(@RequestParam("username") String username,@RequestParam("password") String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        User u = userService.queryUser(user);

        if(u==null){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(u);

    }


}
